#!/bin/sh

. /etc/utils.sh

cpss_bin="cpss.gomain"

create_switch_dev_symlink() {
  # Create a /dev/switch symlink to the PCI device for use in other programs/scripts
  # Currently use a fixed sysfs pci device path. If hardware changes such that this
  # path varies, could do a search based on vendor & device ID.
  rm -f "/dev/switch"
  ln -s "/sys/bus/pci/devices/0000:01:00.0" "/dev/switch"
}

case "$1" in
  start)
    if is-ptp; then
      create_switch_dev_symlink

      # start marvell switch CLI, allow 100KB/s
      babysit 10 cpss_wrapper 2>&1 | logos cpss 0 2000000 &

      # save prestera stats for catawampus, allow 1000KB/s
      babysit 60 prestera_periodic --interval 60 2>&1 | logos prestera 0 20000000 &
    fi
    ;;
  stop)
    # kill cpss and babysitter
    if is-ptp; then
      pkillwait -f prestera_periodic
      pkillwait -f "babysit.*cpss_wrapper"
      pkillwait -x cpss_wrapper
      pkillwait -x $cpss_bin
    fi
    ;;
  restart)
    "$0" stop
    "$0" start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart}"
    exit 1
esac
